<?php

/**

 * Created by PhpStorm.

 * User: Administrator

 * Date: 2018/6/6

 * Time: 11:25

 */



namespace App\Http\Controllers\Home;



use App\Model\GroupList;

use App\Model\Members;

use App\Model\ShopOrder;

use App\Model\ShopOrderComment;

use App\Model\ShoppingGoods;

use Illuminate\Http\Request;

use App\Model\ShopGoods;

use App\Model\ShopGoodsImg;

use App\Model\GoodsSpecsVal;

use App\Model\GoodsSpecsName;

use App\Model\ShopGoodsClassify;

use App\Model\MembersLevel;

use App\Model\MicroMall;

class StraightShopController extends BaseController

{

    /**

     * 直商城首页

     */

    public function index()

    {

        $user = session('home_user');

        if($user['straight_shop_isdown'] !=1){

            return back()->withErrors('没有直商城权限');

        }

        return view('Home.straight.straightShop');

    }

    /**

     * 购买页面

     */

    public function product()

    {

        return view('Home.straight.product');

    }

    /**

     * 订单页面

     */

    public function order()

    {

        return view('Home.straight.order');

    }

    /**

     * 直商城商品表

     * @param Request $request

     * @return array

     * @throws \Exception

     */

    public function goods(Request $request)

    {

        $data = $request->all();

        if(empty($data)){

            return ['code'=>0,'data'=>'参数错误'];

        }

        switch ($data['class']){

            case 'select': //查询

                $table = ShopGoodsClassify::where(['type'=>1])->select('id');

                $classify = $table->get()->toArray();

                $pid = [];

                if (!empty($classify)) {

                    foreach ($classify as $value) {

                        $pid[] = $value['id'];

                    }

                } else {

                    return ['code'=>0,'data'=>'暂时还没有商品'];

                }

                //查询商品

                $shop = ShopGoods::where(['type'=>1,'status'=>1])->whereIn('cid',$pid)->get()->toArray();

                if ($shop){

                    foreach ($shop as $k => $value) {

                        $img = ShopGoodsImg::where(['gid'=>$value['id']])->select('img')->first();

                        $shop[$k]['img'] = $img->img;

                    }

                    return ['code'=>1,'data'=>$shop];

                } else {

                    return ['code'=>0,'data'=>'没有数据'];

                }

                break;

            case 'add':    //添加 or 修改

                if ($data['goods']['id']) { //修改

                    $Rows = ShopGoods::where('id',$data['goods']['id'])->update($data['goods']);

                } else {

                    $data['createtime'] = time();

                    $Rows = ShopGoods::create($data['goods']);

                }

                //更新图片

                $goodsId = empty($data['goods']['id'])?$Rows->id : $data['goods']['id'];

                $goodsImgName = empty($data['goodsImgName'])?0:$data['goodsImgName'];

                $rico = $this->updateImg($goodsId,$goodsImgName);

                if ($Rows || $rico){

                    return ['code'=>1,'data'=>'设置成功'];

                } else {

                    return ['code'=>0,'data'=>'设置失败'];

                }

                break;

            case 'find':   //查询一条数据

//                $group = ShoppingGoods::where('id',$data['id'])->first();

//                if($group){

//                    $good_id = $group['gid'];

//                }else{

//                    return ['code'=>0,'data'=>'暂无数据'];

//                }



                //查出该商品对应的所有规格名

                $GoodsSpecsName = GoodsSpecsName::where( [ 'goods_id' => $data['id'] ] )->select( 'id', 'name' )->get()->toArray();

                //查出该商品的所有规格值

                $GoodsSpecsVal = [];

                if ( $GoodsSpecsName ) {

                    foreach ( $GoodsSpecsName as $k => $v ) {

                        $thisSpecsVal = GoodsSpecsVal::where( [ 'specs_id' => $v['id'] ] )->select( 'id', 'val', 'specs_id' )->get()->toArray();

                        array_push( $GoodsSpecsVal, [ 'specs_id' => $v['id'], 'vals' => $thisSpecsVal ] );

                    }

                }



//                $goods = ShopGoods::where('id',$good_id)->first();

                $goods = ShopGoods::where(['id'=>$data['id']])

                    ->first();

                $goodsImgName = json_decode($goods['rotation_chart']);

                if ($goodsImgName){

                    $goodsList = array();

                    foreach ( $goodsImgName as $k => $v ){

                        $goodsList[$k]['img'] = $v;

                    }

                    $goodsImgName = $goodsList;

                }

                if (empty($goodsImgName)){

                    $goodsImgName = ShopGoodsImg::where('gid',$data['id'])->select('img')->get()->toArray();

                }





                if ($goods){

                    $orderData =  ShopOrderComment::where('goodsid',$data['id'])

                        ->join('shop_order','shop_order.id','=','shop_order_comment.orderid')

                        ->where('shop_order.status','>=',1)

                        ->join('members', 'shop_order_comment.uid', '=', 'members.uid')

                        ->select('members.name','shop_order.createtime')

                        ->orderBy('shop_order.createtime','DESC')

                        ->limit(5)

                        ->get();

                    foreach ($orderData as $k=>$v){

                        $orderData[$k]['createtime'] = date('Y-m-d H:i:s',$v['createtime']);

                        $orderData[$k]['name'] = MOBILE_PHONE_HIDDEN($v['name']);

                    }

                    $user = session('home_user');

                    //直商城

                    $userLeve = MembersLevel::where('id',$user['level'])->first();

                    if(empty($userLeve)){

                        $userLeve = [

                            'level_name'=>0,

                            'conditions'=>0,

                            'credit3'=>0,

                        ];

                    }



                    //微商城

//                    $MicroLeve = MicroMall::where('id',$user['tiny_level'])->first();



                    $count = GroupList::where(['goods_id' =>$data['id'],'pay_status' => 1,'open_state'=>0])->count();

//                    var_dump($data['id']);exit;

                    return ['code'=>1,'data'=>['goods'=>$goods,'goodsImgName'=>$goodsImgName,'userLeve'=>$userLeve,'user'=>$user],'orderData'=>$orderData ,'GoodsSpecsName' => $GoodsSpecsName, 'GoodsSpecsVal' => $GoodsSpecsVal,'count'=>$count ];

                } else {

                    return ['code'=>0,'data'=>'设置失败'];

                }

                break;

            case 'delete': //删除

                if ($data['id']) {

                    $rico = ShopGoods::where('id',$data['id'])->update(['type' => -1]);

                    if ($rico){

                        return ['code'=>1,'data'=>'删除成功'];

                    } else {

                        return ['code'=>0,'data'=>'删除失败'];

                    }

                } else {

                    return ['code'=>0,'data'=>'数据错误'];

                }

                break;

            case 'group_list': //删除

                //此商品已发起团购用户

//                $groups = ShoppingGoods::join('')

                $groups = GroupList::where(['group_id'=>$data['group_id'],'pay_status'=>1,'open_state'=>0])->get()->toArray();

                foreach ($groups as $k=>$v){

                    $shoping = ShoppingGoods::where('id',$data['group_id'])->first();

                    $groups[$k]['poor'] =$shoping['actual_num'] - $v['num'];

                    $groups[$k]['createtime'] =date('Y-m-d H:i:s', $v['createtime']);

                }

                if ($groups){

                    return ['code'=>1,'data'=>$groups];

                } else {

                    return ['code'=>0,'data'=>'暂无拼团，试试发起'];

                }

                break;

        }

    }





}